<?php

class RestController extends Controller
{
    public function beforeAction($action)
    {
        // xhprof_enable();
        return parent::beforeAction($action);
    }
    public function setResult($data)
    {
        if($data)
        {
            $data      = $this->formatResult($data);
            $content   = json_encode($data);
            $spentTime = intval((microtime(true) - YII_BEGIN_TIME)*1000);
            $log       = MOBILEOS.' '.APPVERSION .' app request url '.$this->route.' params are '.json_encode($_REQUEST).' and php result is '.$content.' spent time '.$spentTime.' ms';
            Yii::trace($log,'php.echo');
            if($data['success']===0)
                Yii::log($log,'warning','php.warning.controller');
            $performanceLog = MOBILEOS.' '.APPVERSION .' app request url '.$this->route.' spent '.$spentTime.' ms';
            Yii::log($performanceLog,CLogger::LEVEL_INFO);
            Helper::recordPerformanceLog($spentTime,$performanceLog);

            //性能分析
            /***
            $xhprof_data = xhprof_disable();
            $XHPROF_ROOT = '/home/packages/xhprof-0.9.4';
            include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
            include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
            $xhprof_runs = new XHProfRuns_Default();
            $run_id = $xhprof_runs->save_run($xhprof_data, str_replace('/', '_', $this->route));
            */
            //性能分析结束
            echo $content;
            Yii::app()->end();
        }
    }
}